home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / cstdio.arc / SRC.ARC / MTYBUF.C < prev    next >
C/C++ Source or Header  |  1984-07-23  |  763b  |  39 lines

  1. /*    mtybuf.c - empty buffer to file.
  2.     (C) Copyright 1984 Gregory R. Mansfield - All Rights Reserved.
  3.     G. R. Mansfield.  84/06/05.
  4.     Ver 1.1-4723.
  5. */
  6.  
  7. #include "stdio.h"
  8.  
  9. int _mtybuf(c, fp)
  10. char c;
  11. FILE *fp;
  12. {
  13.     int l;
  14.  
  15.     if ((fp->_flag & _WRITE) == 0)
  16.         return(ERR);
  17.     if (fp->_base == NULL) {    /* find buffer space */
  18.         if (fp->_flag & _UNBUF) {    /* unbuffered */
  19.             write(fp->_fd, &c, 1);
  20.             ++fp->_cnt;
  21.             return(c);
  22.         }
  23.         if ((fp->_base = malloc(BUFSIZE, 1)) == NULL) {
  24.             wcs("no buffer space for file\n");
  25.             exit(2);
  26.         }    
  27.     }
  28.     else {
  29.         l = fp->_ptr - fp->_base;
  30.         if (write(fp->_fd, fp->_base, l) != l) {
  31.             fp->_flag |= _ERR;
  32.             return(ERR);
  33.         }
  34.     }
  35.     fp->_ptr = fp->_base;
  36.     fp->_cnt = BUFSIZE - 1;
  37.     return(*fp->_ptr++ = c);
  38. }
  39.